package org.Help_Desk.Model;

import java.sql.*;
import java.util.ResourceBundle;



import org.Help_Desk.Entity.Employee;

public class DatabaseConnection {

	private String DATABASEDRIVER;
	private String DATABASEURL;
	private Connection cn;
	
	public DatabaseConnection()
	{
		System.out.println("heredb");
	
		ResourceBundle rb=ResourceBundle.getBundle("org.Help_Desk.Model.Config");
				DATABASEDRIVER=rb.getString("DatabaseVendorDriver");
				DATABASEURL=rb.getString("DatabaseURL");
	}
	public void connect() throws ClassNotFoundException, SQLException
	{
		System.out.println("herecn");
			Class.forName(DATABASEDRIVER);
			cn=DriverManager.getConnection(DATABASEURL,"root","");		
	}
	public Employee fetchemployee(int sessionid) throws SQLException
	{
		System.out.println("herefet");
		Employee emp=new Employee();
		try
		{
		Statement stmt = cn.createStatement();
		ResultSet rs = stmt.executeQuery("SELECT Emp_Id,Emp_Fname,Emp_Lname,Emp_Address,Emp_Contact_No,Emp_Email,DOB from employee where Emp_Id='" + sessionid +"' ");
		while( rs.next() ) { 
		emp.setAddress(rs.getString("Emp_Address"));
		emp.setEmailId(rs.getString("Emp_Email"));
		emp.setContactNumber(rs.getString("Emp_Contact_No"));
		
		emp.setFname(rs.getString("Emp_Fname"));
                emp.setLname(rs.getString("Emp_Lname"));
		emp.setDOB(rs.getString("DOB"));
		emp.setEmployeeid(Integer.parseInt(rs.getString("Emp_Id")));
		System.out.println(rs.getString("Emp_Email"));
		}
		
		stmt.close();
		}
		catch(Exception e)
		{
		}
		return emp;
	}
	public void UpdateEmployee(Employee emp) throws SQLException
	{
		System.out.println("hereup");
		String DOB=emp.getDOB();
		String num=emp.getContactNumber();
		String add=emp.getAddress();
		String email=emp.getEmailId();
		int id=emp.getEmployeeid();
		
		System.out.println("hereup1");
		PreparedStatement pst=cn.prepareStatement("UPDATE Employee set DOB='"+ DOB +"',Emp_Contact_No='"+ num +"',Emp_Address='"+ add +"',Emp_Email='"+ email +"' where Emp_Id='"+ id +"'");
		pst.executeUpdate();
		pst.close();
		
	}

	public void disconnect() throws SQLException
	{
		cn.close();
	}
	
}
